﻿using System;
using System.Collections.Generic;
using Unity.UIWidgets.engine;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.gestures;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
using Image = Unity.UIWidgets.widgets.Image;

//https://flutterchina.club/tutorials/interactive/
//根据上面网页里的教学编写的代码
public class MyFirstUIWidgets : UIWidgetsPanel
{
    protected override void OnEnable()
    {
        FontManager.instance.addFont(Resources.Load<Font>(path: "fonts/MaterialIcons-Regular"), "Material Icons");
        base.OnEnable();
    }

    protected override Widget createWidget()
    {
        return new MyApp();
    }

    class MyApp : StatelessWidget
    {
        class FavoriteWidget : StatefulWidget
        {
            public override State createState()
            {
                return new FavoriteState();
            }
        }

        //state的泛型参数填写该状态的widget类型
        class FavoriteState : State<FavoriteWidget>
        {
            private float favoriteCount = 41;
            private bool _isfavorite = true;

            void OnTap()
            {
                print("OnTap");
                //调用setState会触发widget重绘，调用build方法
                //我测试过，改变state的代码写前面也是可以的
                setState(() =>
                {
                    // If the lake is currently favorited, unfavorite it.
                    if (_isfavorite)
                    {
                        favoriteCount -= 1;
                        _isfavorite = false;
                        // Otherwise, favorite it.
                    }
                    else
                    {
                        favoriteCount += 1;
                        _isfavorite = true;
                    }
                });
            }

            public override Widget build(BuildContext context)
            {
                return new Row(
                    mainAxisSize: MainAxisSize.min,
                    children: new List<Widget>
                    {
                        new Container(
                            padding: EdgeInsets.all(0.0f),
                            child: new IconButton(
                                icon: (_isfavorite
                                    ? new Icon(Icons.star)
                                    : new Icon(Icons.star_border)),
                                color: Colors.red[500],
                                onPressed: OnTap
                            )
                        ),
                        new Text(favoriteCount.ToString())
                        //将文本放在SizedBox中并设置其宽度可防止出现明显的“跳跃” ，因为这些值具有不同的宽度
                        // new SizedBox(
                        //     width: 18.0f,
                        //     child: new Container(
                        //         child: new Text(favoriteCount.ToString())
                        //     )
                        // )
                    });
            }
        }

        //自我更新的tapbox
        class TapBoxA : StatefulWidget
        {
            public override State createState()
            {
                return new TapBoxAState();
            }
        }

        class TapBoxAState : State<TapBoxA>
        {
            private bool _active = false;

            public void OnTap()
            {
                _active = !_active;
                setState();

                print("ontasp");
            }

            public void OnPressed()
            {
                print("OnPressed");
            }

            public override Widget build(BuildContext context)
            {
                //手势检测器
                return new GestureDetector(
                    onTap: OnTap,
                    child:
                    new Container(
                        padding: EdgeInsets.all(32),
                        child: new Center(child: new Text(
                            _active ? "Active" : "Inactive",
                            style: new TextStyle(fontSize: 20),
                            textAlign: TextAlign.center //只能让文本水平方向居中，全部居中还得套一个center
                        )),
                        width: 200,
                        height: 200,
                        decoration: new BoxDecoration(color: _active ? Colors.green : Colors.red)
                    )
                );
            }
        }

        class TapBoxBParent : StatefulWidget
        {
            public override State createState()
            {
                return new TapBoxBParentState();
            }
        }

        class TapBoxBParentState : State<TapBoxBParent>
        {
            public bool active;

            public void HandleTap(bool newValue)
            {
                print("HandleTap");
                setState(() => this.active = newValue); //会重新调用下面的build方法
            }

            public override Widget build(BuildContext context)
            {
                return new TapBoxB(active, HandleTap);
            }
        }

        //父widget更新的tapbox
        class TapBoxB : StatelessWidget
        {
            private bool active;
            private ValueChanged<bool> onChange;

            //构造方法结束后 会调用build方法
            public TapBoxB(bool active, ValueChanged<bool> onchange) : base(null)
            {
                print("TapBoxB构造函数");
                this.active = active;
                this.onChange = onchange;
            }

            public void OnTapboxBTap()
            {
                onChange(!active);
            }

            public override Widget build(BuildContext context)
            {
                print("TapBoxB build");
                return new GestureDetector(
                    onTap: OnTapboxBTap,
                    child: new Container(
                        child: new Center(
                            child: new Text(
                                active ? "active" : "inactive",
                                style: new TextStyle(fontSize: 20)
                            )
                        ),
                        width: 200,
                        height: 200,
                        decoration: new BoxDecoration(
                            color: active ? Colors.green : Colors.red
                        )
                    )
                );
            }
        }

        //tapboxc 混合管理  tapboxc高亮由自己管理，active还是由父亲widget管理

        class TapBoxCParent : StatefulWidget
        {
            public override State createState()
            {
                return new TapBoxCParentState();
            }
        }

        class TapBoxCParentState : State<TapBoxCParent>
        {
            private bool _tapboxCActive = false;

            public void HandleOnChange(bool newValue)
            {
                setState(() => _tapboxCActive = newValue);
            }

            public override Widget build(BuildContext context)
            {
                return new TapBoxC(_tapboxCActive, HandleOnChange);
            }
        }

        class TapBoxC : StatefulWidget
        {
            //需要弄到构造方法里 弄出去让父widget管理的state
            public bool _active;
            public ValueChanged<bool> OnValueChange;

            public TapBoxC(bool active, ValueChanged<bool> onValueChange) : base(null)
            {
                this._active = active;
                this.OnValueChange = onValueChange;
            }

            public override State createState()
            {
                return new TapBoxCState();
            }
        }

        class TapBoxCState : State<TapBoxC>
        {
            //自己管理的状态
            private bool _highLight = false;

            void HandleOnTap()
            {
                //可以直接用widget访问该state的widget
                //通知父widget，我触发事件了
                widget.OnValueChange(!widget._active);

                print("HandleOnTap");
            }

            void HandleOnTapDown(TapDownDetails details)
            {
                _highLight = true;
                setState();
                print("HandleOnTapDown");
            }

            void HandleOnTapUp(TapUpDetails details)
            {
                _highLight = false;
                setState();
                print("HandleOnTapUp");
            }

            void HandleOnTapCancel()
            {
                _highLight = false;
                setState();
                print("HandleOnTapCancel");
            }

            public override Widget build(BuildContext context)
            {
                return new GestureDetector(
                    onTap: HandleOnTap,
                    onTapCancel: HandleOnTapCancel,
                    onTapDown: HandleOnTapDown,
                    onTapUp: HandleOnTapUp,
                    child: new Container(
                        child: new Center(
                            child: new Text(
                                widget._active ? "Active" : "inactive",
                                style: new TextStyle(fontSize: 20)
                            ))
                        ,
                        width: 200,
                        height: 200,
                        decoration: new BoxDecoration(
                            color: widget._active ? Colors.green : Colors.red,
                            border: _highLight ? Border.all(color: Colors.teal[700], width: 10.0f) : null
                        )
                    )
                );
            }
        }

        public override Widget build(BuildContext context)
        {
            //标题部分
            Widget titleSection = new Container(
                padding: EdgeInsets.all(32.0f),
                child:
                new Row(
                    children: new List<Widget>
                    {
                        new Expanded(
                            child: new Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                children: new List<Widget>()
                                {
                                    new Container(
                                        //padding: EdgeInsets.only(bottom: 8.0f),
                                        child:
                                        new Text(
                                            "Oeschinen Lake Campground",
                                            style: new TextStyle(
                                                fontWeight: FontWeight.bold
                                            )
                                        )
                                    ),
                                    new Text(
                                        "Kandersteg, Switzerland",
                                        style: new TextStyle(
                                            color: Colors.grey[500]
                                        )
                                    )
                                }
                            )
                        ),
                        //点赞按钮
                        new FavoriteWidget()
                        // new Icon(
                        //     Icons.star,
                        //     color: Colors.red[500]
                        // ),
                        // new Text("41")
                    }
                )
            );
            //按钮部分
            Widget buttonSection = new Container(
                child: new Row(
                    mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                    children: new List<Widget>
                    {
                        buildButtonColumn(Icons.call, "CALL"),
                        buildButtonColumn(Icons.near_me, "ROUTE"),
                        buildButtonColumn(Icons.share, "SHARE")
                    }
                )
            );

            Column buildButtonColumn(IconData icon, String label)
            {
                Color color = Theme.of(context).primaryColor;

                return new Column(
                    mainAxisSize: MainAxisSize.min,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: new List<Widget>()
                    {
                        new Icon(icon, color: color),
                        new Container(
                            margin: EdgeInsets.only(top: 8.0f),
                            child: new Text(
                                label,
                                style: new TextStyle(
                                    fontSize: 12.0f,
                                    fontWeight: FontWeight.w400,
                                    color: color
                                )
                            )
                        )
                    });
            }

            //文本部分
            Widget textSection = new Container(
                padding: EdgeInsets.all(32.0f),
                child: new Text(
                    "Lake Oeschinen lies at the foot of the Blüemlisalp in the Bernese Alps. Situated 1,578 meters above sea level, it is one of the larger Alpine Lakes. A gondola ride from Kandersteg, followed by a half-hour walk through pastures and pine forest, leads you to the lake, which warms to 20 degrees Celsius in the summer. Activities enjoyed here include rowing, and riding the summer toboggan run.",
                    softWrap: true
                )
            );


            return new MaterialApp(
                title: "Demo",
                theme: new ThemeData(
                    primarySwatch: Colors.blue),
                home: new Scaffold(
                    appBar: new AppBar(
                        title: new Text("Startup Name Generator")
                    ),
                    body: new ListView(
                        children: new List<Widget>
                        {
                            new Row(
                                children: new List<Widget>()
                                {
                                    new Center(
                                        child: new TapBoxA()
                                    ),
                                    new Center(
                                        child: new TapBoxBParent()
                                    ),
                                    new Center(
                                        child: new TapBoxCParent()
                                    )
                                },
                                mainAxisAlignment: MainAxisAlignment.center
                            ),
                            Image.asset("testimage"
                                , width: 600.0f,
                                height: 240.0f,
                                fit: BoxFit.cover),
                            titleSection,
                            buttonSection,
                            textSection,
                        }
                    )
                )
            );
        }
    }
}